// pages/detail/detail.js
const { getMaintenanceDetail } = require("../../utils/api");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myTitle:"故障详情",
    isBg:false,
    list:['打火','破损','磨损','烧伤'],
    navBarHeight:app.globalData.navBarHeight,
    userInfo: null,
    maintenanceNumber: '',
    maintenanceDetail: [],
    sourceNavigate: '',
    faultReason: {
      popupIndex: null,
    },
  },
  onPageScroll(e){
    this.setData({
      isBg : e.scrollTop < 40 ?false : true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      maintenanceNumber: options.maintenanceNumber,
      sourceNavigate: options.sourceNavigate,
      userInfo: wx.getStorageSync('userInfo')
    });
    this.getMaintenanceDetail();
  },

  handelback(){
    wx.navigateBack({ 
      delta:1
    })
  },
  changeUniteListPopupHidden(e) {
    const index = e.currentTarget.dataset.index;
    if (this.data.faultReason.popupIndex === null && this.data.maintenanceDetail.FaultReasonList[index].MultipleSelections) {
      this.setData({
        'faultReason.popupIndex': index
      })
    } else {
      this.setData({
        'faultReason.popupIndex': null
      })
    }
  },

  getMaintenanceDetail: function () {
    const data = { PhoneNumber: this.data.userInfo.PhoneNumber, MaintenanceNumber: this.data.maintenanceNumber, };
    console.log(data)
    getMaintenanceDetail(data).then(res => {
      if (res.code == 4002050) {
        const imageToExtract = ['FaultPhotoFirst', 'FaultPhotoSecond', 'FaultPhotoThird', 'FaultPhotoFourth'];
        const imageList = imageToExtract.map(key => res.data[key]).filter(image => image !== undefined && image !== null);
        imageToExtract.forEach(key => delete res.data[key]);
        res.data.postImageList = imageList;
        const imageToReturn = ['DetectionResultFirstPhoto', 'DetectionResultThirdPhoto', 'DetectionResultSecondPhoto', 'DetectionResultFourthPhoto'];
        const imageReturnList = imageToExtract.map(key => res.data[key]).filter(image => image !== undefined && image !== null);
        imageToReturn.forEach(key => delete res.data[key]);
        res.data.returnImageList = imageReturnList;
        this.setData({
          maintenanceDetail: res.data,
        });
      }
    });
  },
})